;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.InnerOrder", [])
        .controller("InnerOrderCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, UserContextService) {
            $scope.activeFlag = [true, false];
            $scope.submitted = false;
            $scope.condition = {};
            $scope.dataList = [];
            $scope.depotPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };


            $scope.detailInfo = {};
            $scope.depotTabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };

            var btnPermissionCode = {
                InnerOrderAdd: window.ActionCode.InnerOrderAdd,
                InnerOrderEdit: window.ActionCode.InnerOrderEdit,
                InnerOrderDel: window.ActionCode.InnerOrderDel,
            
            };

            $scope.btnPermission = {
                InnerOrderAdd: true,
                InnerOrderEdit: true,
                InnerOrderDel: true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    // $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                    $scope.btnPermission[key] = true;
                }
            }

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    case 2:
                        $scope.placeTabFlag.areaNo = "";
                        $scope.condition.placeNo = "";
                        $scope.detailInfo = {};
                        break;
                    case 3:
                        break;
                    case 4:
                        $scope.detailInfo = {};
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.tabFlag = {};
                $scope.detailInfo = {};
            }

            $scope.getDataList = function () {
                var service = "device";
                var reqUrl = "/innerOrder/list";
                var orderNo = $scope.condition.orderNo;            
               
                if(  orderNo === null || orderNo  === undefined )
                {
                    orderNo= "";
                }

                var param = {orderNo: orderNo};
                console.log(param);
                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.dataList = resp.value;
                });
            }


            $scope.search = function () {
                $scope.getDataList();
            };

            $scope.removeDepot = function (id) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/innerOrder/remove";
                    var param = {id: id};
                    ybHttp.Get(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        $scope.getDataList();
                    });
                }, function () {

                });
            };


            $scope.addData = function () {
                $scope.depotTabFlag.type = 0;
                $scope.depotTabFlag.name = tabFlag[0];
                $scope.changeTab(1);
            };
            $scope.editData = function (id) {
                var service = "device";
                var reqUrl = "/innerOrder/detail";
                var param = {id: id};

                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.detailInfo = resp.value;
                    $scope.depotTabFlag.type = 1;
                    $scope.depotTabFlag.name = tabFlag[1];
                    $scope.changeTab(1);
                });
            };

            function changeTab(index, flag, storageType) {
                $scope.activeFlag[index] = false;
            }

            $scope.cancel = function (index, flag) {
                changeTab(index, flag);
                $scope.detailInfo = {};
            };

            $scope.saveData = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var param = angular.copy($scope.detailInfo);
                var reqUrl = "/innerOrder/insert";
                if ($scope.depotTabFlag.type === 1) {
                    reqUrl = "/innerOrder/update";
                    param.id = $scope.detailInfo.id;
                }

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    $scope.detailInfo = {};
                    $scope.getDataList();
                });
            };

            (function () {
                $scope.getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);
